package pt.isel.pdm.ouryamba;

import winterwell.jtwitter.Twitter;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment implements IMyListener, Runnable
{
	private General app;
	private View view;
	private TextView id;
	private TextView author;
	private TextView date;
	private TextView text;
	private final Integer onSuccessToken = 0;
	
	
//IMyListener methods

	/**
	 * Perform some actions due to the service execution success.
	 * 
	 * Since the call to this method will be done by one thread from the service (not the UI thread)
	 * we need to delegate some actions to be taken in the UI thread.
	 * 
	 * So we use one Runnable type to define those actions and we send that Runnable to be ran on
	 * the UI thread, with the {@code getActivity().runOnUiThread(onSuccessThread)} call.
	 * 
	 * When the service successfully fetches the statuses, the views from this fragment should be
	 * updated with the fresh statuses info.
	 * */
	public void onSuccess(Integer token)
	{
		if(token != onSuccessToken)
		{
			return;
		}
		getActivity().runOnUiThread(this);
	}
	
	
//Runnable methods
	
	/**
	 * Delegate the actions to be taken on the UI thread when the service notifies this fragment.
	 * 
	 * It simple updates all the views of this fragment with the information of one new fetched 
	 * status.
	 * 
	 * @see #updateViewElements(boolean)
	 * */
	@Override
	public void run()
	{
		updateViewElements(false);
	}

	
//Own methods
	
	/**
	 * Update all the views of this fragment.
	 * 
	 * Depending on the parameter, update the views with the clicked status 
	 * ({@code toRefreshing} false) or the with word "refreshing" indicating the status is not
	 * available yet ({@code toRefreshing} true).
	 * 
	 * One detail important of this code is that the date from the status is print alike the region
	 * of the device (US, European, etc.) with the use of the {@code toLocaleString()} call.
	 * The method is deprecated but its usage is very useful and we didn't found any "substitute"
	 * for this method.
	 * 
	 * @param toRefreshing Indicates whether the views are to be updated with a status or not.
	 * @see java.util.Date#toLocaleString()
	 * */
	@SuppressWarnings("deprecation")
	private void fillAllTextViews(boolean toRefreshing)
	{
		String _id, _author, _date, _text;
		if(toRefreshing)
		{
			_id = _author = _date = _text = getString(R.string.refreshing);
		}
		else
		{
			Twitter.Status status = app.listOfStatuses.get(app.statusSelectedPosition);
			_id = String.valueOf(status.id);
			_author = status.user.name;
			_date = status.createdAt.toLocaleString();
			_text = status.text;
		}
		id.setText(app.SPACE_STRING + _id);
		author.setText(app.SPACE_STRING + _author);
		date.setText(app.SPACE_STRING + _date);
		text.setText(app.SPACE_STRING + _text);
	}
	
	/**
	 * Before updating the views, store their references, if they were not stored yet.
	 * 
	 * This method is only to ensure that the views are not unreferenced before we update them,
	 * which would probably thrown a {@code NullPointerException}.
	 * 
	 * @param toRefreshing Indicates whether the views are to be updated with a status or not.
	 * @see #referenceComponents()
	 * @see #fillAllTextViews(boolean)
	 * */
	public void updateViewElements(boolean toRefreshing)
	{
		if(id == null)
		{
			referenceComponents();
		}
		fillAllTextViews(toRefreshing);
	}
	
	/**
	 * Fetch the reference of all the views of this fragment.
	 * */
	public void referenceComponents()
	{
		id = (TextView) view.findViewById(R.id.id_value_tv);
		author = (TextView) view.findViewById(R.id.author_value_tv);
		date = (TextView) view.findViewById(R.id.date_value_tv);
		text = (TextView) view.findViewById(R.id.text_value_tv);
	}
	
	
//Activity methods
	
	/**
	 * Inflates the fragment layout and store the reference for the General class.
	 * 
	 * Why this class don't inherit from BaseActivity?
	 * Because there was no need for that. This class does not reflect the behavior that the 
	 * BaseActivity's children activities should have and also because this is a fragment, not
	 * a activity.
	 * */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		view = inflater.inflate(R.layout.fragment_detail, container);
		app = (General) getActivity().getApplication();
		return view;
	}

	/**
	 * Once the activity is created it reference the components and if the fragment is inside the
	 * DetailActivity, updates the views with a selected status.
	 * 
	 * @see #referenceComponents()
	 * @see #updateViewElements(boolean)
	 * */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		referenceComponents();
		if(getActivity() instanceof DetailActivity)
		{
			updateViewElements(false);
		}
	}
	
}
